/*
 * Copyright 2006 Kevin Wetzels 
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); 
 * you may not use this file except in compliance with the License. 
 * You may obtain a copy of the License at 
 * 
 *       http://www.apache.org/licenses/LICENSE-2.0 
 *       
 * Unless required by applicable law or agreed to in writing, software 
 * distributed under the License is distributed on an "AS IS" BASIS, 
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
 * See the License for the specific language governing permissions and 
 * limitations under the License.
 */
package be.roam.util;

/**
 * Utility methods for dealing with strings.
 * 
 * @author Kevin Wetzels
 * @version 1.0
 */
public class StringUtil {

    public static boolean isNullOrEmpty(String string) {
        return string == null || "".equals(string.trim());
    }

    public static boolean parseBoolean(String booleanString, boolean defaultValue) {
        return (isNullOrEmpty(booleanString) ? defaultValue : "1".equals(booleanString) || "true".equalsIgnoreCase(booleanString));
    }

    public static boolean parseBoolean(String booleanString) {
        return parseBoolean(booleanString, false);
    }

    public static int parseInt(String intString, int defaultValue) {
        return (isNullOrEmpty(intString) ? defaultValue : Integer.parseInt(intString));
    }

    public static int parseInt(String intString) {
        return parseInt(intString, 0);
    }

    public static long parseLong(String longString, long defaultValue) {
        return (isNullOrEmpty(longString) ? defaultValue : Long.parseLong(longString));
    }

    public static long parseLong(String longString) {
        return parseLong(longString, 0L);
    }
    
    public static double parseDouble(String doubleString, double defaultValue) {
        return (isNullOrEmpty(doubleString) ? defaultValue : Double.parseDouble(doubleString));
    }
    
    public static double parseDouble(String doubleString) {
        return parseDouble(doubleString, 0.0);
    }
    
    private StringUtil() {
        super();
    }
    
}
